const TOKEN_KEY = 'auth_token'
const USER_INFO_KEY = 'user_info'

export interface UserInfo {
  id: number
  phone: string
  nickname: string
  avatar: string
}

// 获取token
export const getToken = (): string | null => {
  return uni.getStorageSync(TOKEN_KEY)
}

// 设置token
export const setToken = (token: string): void => {
  uni.setStorageSync(TOKEN_KEY, token)
}

// 移除token
export const removeToken = (): void => {
  uni.removeStorageSync(TOKEN_KEY)
}

// 获取用户信息
export const getUserInfo = (): UserInfo | null => {
  const userInfo = uni.getStorageSync(USER_INFO_KEY)
  return userInfo ? JSON.parse(userInfo) : null
}

// 设置用户信息
export const setUserInfo = (userInfo: UserInfo): void => {
  uni.setStorageSync(USER_INFO_KEY, JSON.stringify(userInfo))
}

// 移除用户信息
export const removeUserInfo = (): void => {
  uni.removeStorageSync(USER_INFO_KEY)
}

// 检查是否已登录
export const isLoggedIn = (): boolean => {
  return !!getToken() && !!getUserInfo()
}

// 清除所有认证信息
export const clearAuth = (): void => {
  removeToken()
  removeUserInfo()
}
